package com.zcb.agrimarket.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zcb.agrimarket.pojo.RepairLog;
import com.zcb.agrimarket.result.Result;
import com.zcb.agrimarket.service.LabService;
import com.zcb.agrimarket.service.RepairLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zjf
 * @since 2024-04-05
 */
@RestController
@RequestMapping("/repair_log")
@Api(tags = "报修相关接口")
@Slf4j
@CrossOrigin//$$$用于处理跨域问题
public class RepairController {

    @Autowired
    RepairLogService repairLogService;

    @Autowired
    LabService labService;

    @GetMapping("/{id}")
    @ApiOperation("单个查询报修记录")
    public Result<RepairLog> getbyid(@PathVariable Long id){
        log.info("将要查询的id为：{}",id);
        RepairLog repairLog = repairLogService.getById(id);
        return Result.success(repairLog);
    }

    @PostMapping("/new")
    @ApiOperation("教师功能-新增报修")
    public Result addRepairLog(@RequestBody RepairLog repairLog){
        repairLogService.save(repairLog);
        labService.updateRepairState(repairLog.getLabId(),1);
        return Result.success("成功添加报修记录");
    }

    @PostMapping("/beginRepair")
    @ApiOperation("实验员功能-开始维修")
    public Result beginRepair(@RequestBody RepairLog repairLog){
        repairLogService.updateLogState(repairLog.getId(),2);
        return Result.success("开始维修");
    }

    @PostMapping("/finishRepair")
    @ApiOperation("实验员功能-完成维修")
    public Result finishRepair(@RequestBody RepairLog repairLog){
        //填写维修说明
        repairLogService.updateDescription(repairLog.getId(),repairLog.getRepairDescription());
        //log状态改为已维修
        repairLogService.updateLogState(repairLog.getId(),1);
        //实验室状态该为正常
        labService.updateRepairState(repairLog.getLabId(),0);
        return Result.success("完成维修");
    }

    //todo 还有问题，跟着user的抄过来的，条件用不了
    @GetMapping()
    @ApiOperation("分页查询报修记录")
    public Result<Page> getAll(Integer page, Integer pageSize, String labName){
        log.info("当前页码：{}",page);
        log.info("页面大小：{}",pageSize);
        log.info("查询条件：{}",labName);
        //构造分页构造器
        Page pageInfo = new Page(page,pageSize);

        //构造条件构造器
        LambdaQueryWrapper<RepairLog> queryWrapper = new LambdaQueryWrapper();
        //添加过滤条件
        queryWrapper.like(StringUtils.isNotEmpty(labName),RepairLog::getLabName,labName);

        //执行查询
        repairLogService.page(pageInfo,queryWrapper);

        return Result.success(pageInfo);
    }
}

